package asia.doing.controller.user;

import asia.doing.common.Result;
import asia.doing.entity.Blog;
import asia.doing.service.user.FrontBlogService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 博客信息前端操作接口
 **/
@RestController(value = "frontBlogController")
@RequestMapping("/front/blog")
public class FrontBlogController {

    @Resource
    private FrontBlogService frontBlogService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Blog blog) {
        frontBlogService.add(blog);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        frontBlogService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        frontBlogService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Blog blog) {
        frontBlogService.updateById(blog);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        Blog blog = frontBlogService.selectById(id);
        return Result.success(blog);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Blog blog) {
        List<Blog> list = frontBlogService.selectAll(blog);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Blog blog,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Blog> page = frontBlogService.selectPage(blog, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 根据浏览量来排名
     * @return
     */
    @GetMapping("/selectTop")
    public Result selectTop(){
        List<Blog> list = frontBlogService.selectTop();
        return Result.success(list);
    }

    /**
     * 根据标签推荐其他有该标签的博客文章
     */
    @GetMapping("/selectRecommend/{blogId}")
    public Result selectRecommend(@PathVariable Long blogId) {
        Set<Blog> blogSet = frontBlogService.selectRecommend(blogId);
        return Result.success(blogSet);
    }





    /* 个人用户页面查询对应的信息 */
    /**
     * 根据用户id查询出所有该用户的文章
     */
    @GetMapping("/selectUser")
    public Result selectByUserId(Blog blog,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Blog> page = frontBlogService.selectByUserId(blog, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 分页查询当前用户点赞的博客列表
     */
    @GetMapping("/selectLike")
    public Result selectLike(Blog blog,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Blog> page = frontBlogService.selectLike(blog, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 分页查询当前用户收藏的博客列表
     */
    @GetMapping("/selectCollect")
    public Result selectCollect(Blog blog,
                                @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Blog> page = frontBlogService.selectCollect(blog, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 分页查询当前用户评论的博客列表
     */
    @GetMapping("/selectComment")
    public Result selectComment(Blog blog,
                                @RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Blog> page = frontBlogService.selectComment(blog, pageNum, pageSize);
        return Result.success(page);
    }


}
